package com.example.lin.scrolllinster;

import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = MainActivity.class.getSimpleName();
    private RecyclerView rv;
    private View iv;
    private Toolbar toolbar;
    private View app_bar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        app_bar = findViewById(R.id.app_bar);


        toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        iv = findViewById(R.id.iv);
        initRecyclerView();


    }

    private void initRecyclerView() {
        rv = findViewById(R.id.rv);
        rv.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        rv.addOnScrollListener(new RecyclerView.OnScrollListener() {

            /**
             * 滚动状态发生改变的时候,该方法被调用
             * @param recyclerView
             * @param newState 状态  One of {@link RecyclerView#SCROLL_STATE_IDLE},
             *                     {@link RecyclerView#SCROLL_STATE_DRAGGING} or {@link RecyclerView#SCROLL_STATE_SETTLING}.
             */
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                switch (newState) {
                    case RecyclerView.SCROLL_STATE_IDLE:
                        //滚动停止的状态
                        break;
                    case RecyclerView.SCROLL_STATE_DRAGGING:
                        //用户拖动时的状态
                        break;
                    case RecyclerView.SCROLL_STATE_SETTLING:
                        //不是用户拖动,但正在滚动中的状态
                        break;
                }
                Log.i(TAG, "onScrollStateChanged: " + newState);
            }

            /**
             * 当RecyclerView 滚动的时候,该方法被调用
             * @param recyclerView
             * @param dx 每次滚动的横向偏移量
             * @param dy 每次滚动的纵向偏移量
             */
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                sumDy += dy;
                Log.d(TAG, "onScrolled: " + dy + " , " + sumDy);

                if (sumDy >= 2000 && lastSumDy < 2000) {
                    iv.setVisibility(View.VISIBLE);
                    iv.startAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.show));
                } else if (sumDy <= 2000 && lastSumDy > 2000) {
                    iv.setVisibility(View.GONE);
                    iv.startAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.hide));
                }

                //处理 Toolbar 显示隐藏

                if (sumDy >= 1000) {

                    float v = (sumDy - 1000) / 500.0f ;

                    app_bar.setAlpha(v>=1?1:v);
                }

                if (sumDy >= 1000 && lastSumDy < 1000) {
                    toolbar.setVisibility(View.VISIBLE);
                } else if (sumDy <= 1000 && lastSumDy > 1000) {
                    toolbar.setVisibility(View.GONE);
                }

                lastSumDy = sumDy;
            }

            private int sumDy = 0;
            private int lastSumDy = 0;

        });


        MyAdapter myAdapter = new MyAdapter();
        rv.setAdapter(myAdapter);

    }

    class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {


        @NonNull
        @Override
        public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new MyViewHolder(getLayoutInflater().inflate(android.R.layout.simple_list_item_1, parent, false));
        }

        @Override
        public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
            TextView tv = (TextView) holder.itemView;
            tv.setText("Item " + position);
        }

        @Override
        public int getItemCount() {
            return 100;
        }

        class MyViewHolder extends RecyclerView.ViewHolder {

            public MyViewHolder(View itemView) {
                super(itemView);
            }
        }


    }
}
